﻿var index = -1;
var presstime = new Date().getTime();

$(function () {
    $(".brand-list").keyup(function () { processKeyup(); })
    $(".brand-list").keydown(function () { processKeydown(); })
    $("#TaskQuery_queryBtn").click(function () { brandListLiHide(); })
    //$("#query").focus(function(){processAjaxRequest();})
    //$("#query").blur(function(){brandListLiHide();})
    /*$("#query").keydown(function(){							 
		var txt_query = $("#query").val().replace(/[ ]/g, "");
		if(txt_query=="")return;
		var e = event.keyCode;
		if(e==13){
			brandListLiHide();
			PostHtml();
		}
	})*/
})
function brandListLiHide() {
    $("#brand-list-li").hide();
}
function processAjaxRequest() {
    //var txt_query = $("#query").val().replace(/[ ]/g, "");
    var txt_query = $("#query").val();
    var titleID = $("#hidUID").val();
    if (txt_query == "") {
        brandListLiHide();
        return;
    } else {
        var re_txt_query = $("#query").attr("relve");
        if (txt_query == re_txt_query) return;
    }

    if (new Date().getTime() - presstime < 50) return;
    presstime = new Date().getTime();
    //alert(txt_query);
    $.ajax({
        type: "Post",
        url: "../../../AjxaFiles/AgentQueryServices.ashx",
        data: "query=" + txt_query + "&titleID=" + titleID,

        success: function (msg) {
            if (msg != "") {
                msg = eval(msg);
                var html = "";
                for (var i = 0; i < msg.length; i++) {
                    html += "<li id='" + i + "' sid='" + msg[i].ID + "' onmouseover='overChangeC(" + i + ")' onmouseout='outChangeC()' onClick=setInputVal(" + i + ",0," + msg[i].Price + "," + msg[i].GoodsUnit + "," + msg[i].goodsNum + "," + msg[i].GoodsID + "," + msg[i].GoodsType + "," + msg[i].totalnum + ")>" + msg[i].STitle + "</li>";
                }
                $("#brand-list-li").html(html);
                $("#brand-list-li").slideDown("fast");
            }
        }
    });
}

function processKeyup() {
    var keyCode = event.keyCode;
    if (keyCode == 38 || keyCode == 40) {
        return;
    } else if (keyCode == 13) {
        processEnter();
        index = -1;
    } else {
        processAjaxRequest();
        index = -1;
    }
}

function processKeydown() {
    var keyCode = event.keyCode;
    if (keyCode == 38 || keyCode == 40) processKeyUpAndDown(keyCode);
}

function processKeyUpAndDown(keyCode) {
    var brand = $('#brand-list-li').children();
    var length = brand.length;
    if (length <= 0) return;
    if (index != -1) outChangeC();

    if (keyCode == 38)
        index <= 0 ? index = length - 1 : index--;
    else
        index >= length - 1 ? index = 0 : index++;

    overChangeC(index);
    setInputVal(index, 0, 0, 0, 0, 0, 0, 0);
}


function processEnter() {
    if (index != -1) setInputVal(index, 0, 0, 0, 0, 0, 0, 0);
}

function setInputVal(index, o, price, Unit, goodsNum, GoodsID, GoodsType, totalnum) {
    var v = $('#brand-list-li').children().eq(index).html();
    $('#query').attr("value", v);
    $('#query').attr("p", price);
    $('#query').attr("i", Unit);
    $('#query').attr("g", goodsNum);
    $('#query').attr("gid", GoodsID);
    $('#query').attr("GoodsType", GoodsType);
    $('#query').attr("kc", totalnum);
    $('#query').attr("name", $('#brand-list-li').children().eq(index).attr("sid"));
    if (o == 0) {
        brandListLiHide();
        //PostHtml();
    }
}

function overChangeC(i) {
    outChangeC();
    var brand = $('#brand-list-li').children();
    brand.eq(i).css("background", "#eceff1");
}

function outChangeC() {
    $("#brand-list-li li").css("background", "#fff");
}

$(".brand-list .iSselected").click(function () {
    var input = $(this).children("input");
    var type1 = input.attr("type1");
    if (input.attr("checked") == "checked") {
        input.attr("checked", false);
    } else {
        $("input[" + type1 + "=" + type1 + "]").attr("checked", false);
        input.attr("checked", true);
    }
    $(".brand-list .iSselected").each(function () {
        if ($(this).children("input").attr("checked") == "checked") {
            $(this).addClass("active");
        } else {
            $(this).removeClass("active");
        }
    })
    PostHtml();
});


function showAllBrand() {
    $("#brand a").show();
}
function showBrand() {
    var showTopNum = 5;//设置品牌展示该数量商品后开始展示更多按钮
    var brand = $("#brand a");
    var length = brand.length;
    if (length <= showTopNum) {
        $("#brand .more").hide();
    } else {
        brand.each(function (i) {
            if (i < showTopNum)
                $("#brand a").eq(i).show();
            else
                $("#brand a").eq(i).hide();
        })
        $("a[md='showAll']").show();
    }
}
//showBrand();
$(".brand-list .more").click(function () {
    $(this).hide();
    $(this).siblings(".more").show();
    var md = $(this).attr("md");
    if (md == "showAll") showAllBrand();
    if (md == "show") showBrand();
});